﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;
using System.Data;
using DevExpress.Web.Mvc;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")] 
    public class DeptController : AdminBaseController
    {
        /// <summary>
        /// 部门管理
        /// </summary>
        /// <returns>返回部门管理主页面</returns>
        [AuthorityActionFilter("m_HRDept", Order = 1)]
        [LogPageActionFilter("部门管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 部门树
        /// </summary>
        /// <returns>返回部门树视图</returns>
        public ActionResult DeptTree(string focusNodeName = "")
        {
            BLL.HRDept deptBll = new BLL.HRDept();
            TreeModelList treeModelList = new TreeModelList();

            List<Model.HRDept> deptAllList = new List<Model.HRDept>();
            deptAllList = deptBll.GetListByEntityNo(GlobalConstant.ENTITYNO);
            if (deptAllList.Count > 0)
            {
                List<Model.HRDept> deptlist = (from c in deptAllList where c.FParentDeptNo == null orderby c.FSeq select c).ToList();

                foreach (Model.HRDept dept in deptlist)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = dept.FDeptNo,
                        Name = dept.FDeptName,
                        ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                    };

                    List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();
                    if (deptModelList.Count > 0)
                    {
                        GetChildDept(deptModelList, treeParentItemModel, deptAllList);
                    }

                    treeModelList.Add(treeParentItemModel);
                }

            }
            ViewData["focusNodeName"] = focusNodeName;
            return View(treeModelList);
        }

        /// <summary>
        /// 递归获取部门树
        /// </summary>
        /// <param name="deptChildList">部门列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="deptAllList">部门集合</param>
        private void GetChildDept(List<Model.HRDept> deptChildList, TreeItemModel treeParentItemModel, List<Model.HRDept> deptAllList)
        {
            foreach (Model.HRDept dept in deptChildList)
            {

                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = dept.FDeptNo,
                    Name = dept.FDeptName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();

                if (deptModelList.Count > 0)
                {
                    GetChildDept(deptModelList, treeChildItemModel, deptAllList);
                }

            }

        }

        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <param name="deptNo">部门编号，新增为null</param>
        /// <param name="addType">增加类型 增同级 T ，增下级 X,修改为Null </param>
        /// <returns>返回部门增加或修改视图</returns>
        public ActionResult DeptContent(string deptNo, string addType)
        {
            Model.HRDept dept = new Model.HRDept();
            List<Model.HRPosition> modelList = new List<Model.HRPosition>();
            bool isAdd = true;
            if (null != deptNo)
            {
                deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);

                BLL.HRDept deptBll = new BLL.HRDept();
                dept = deptBll.GetModel(GlobalConstant.ENTITYNO, deptNo);
                
                if (null != dept)
                {
                    if (null == addType)//修改
                    {
                        isAdd = false;
                        if (string.IsNullOrEmpty(dept.FParentDeptNo))
                        {
                            dept.FParentDeptName = "根目录";
                        }
                        else
                        {
                            dept.FParentDeptName = deptBll.GetDeptName(GlobalConstant.ENTITYNO, dept.FParentDeptNo);
                        }

                        if (!string.IsNullOrEmpty(dept.FChargeLeader))
                        {
                            BLL.HREmpl empBll = new BLL.HREmpl();
                            dept.FChargeLeaderName = empBll.GetEmpName(GlobalConstant.ENTITYNO, dept.FChargeLeader);
                        }

                        //获取岗位数据
                        BLL.HRPosition positionBll = new BLL.HRPosition();
                        modelList = positionBll.GetListByDeptNo(deptNo, GlobalConstant.ENTITYNO);
                        if (modelList.Count() > 0)
                        {
                            modelList.ForEach(c => c.FPositionPropName = c.FPositionProp == "1" ? "正级(主持工作)" : c.FPositionProp == "2" ? "副级" : "普通");
                        }

                    }
                    else//增加同级或下级
                    {
                        string parentDeptNo = dept.FParentDeptNo;
                        string deptName = dept.FDeptName;
                        dept = new Model.HRDept();
                        //if (addType == "T")
                        //{
                        //    dept.FParentDeptNo = parentDeptNo;
                        //    if (parentDeptNo == string.Empty)
                        //    {
                        //        dept.FParentDeptName = "根目录";
                        //    }
                        //    else
                        //    {
                        //        dept.FParentDeptName = deptBll.GetDeptName(GlobalConstant.ENTITYNO, dept.FParentDeptNo);
                        //    }
                        //}
                        //else if (addType == "X")
                        //{
                            dept.FParentDeptNo = deptNo;
                            dept.FParentDeptName = deptName;
                        //}

                        dept.FDeptProp = "1";
                        dept.FIsAgency = "N";
                    }

                    OperateSession.addList(GlobalConstant.PARAM_DEPTPOSITION, modelList);
                }
                else
                {
                    dept = new Model.HRDept();
                    dept.FDeptProp = "1";
                    dept.FIsAgency = "N";
                    dept.FParentDeptName = "根目录";
                    OperateSession.addList(GlobalConstant.PARAM_DEPTPOSITION, modelList);
                }

            }
            else
            {
                dept.FDeptProp = "1";
                dept.FIsAgency = "N";
                dept.FParentDeptName = "根目录";
                OperateSession.addList(GlobalConstant.PARAM_DEPTPOSITION, modelList);
            }

            ViewData["bl_deptno"] = isAdd;


            ViewData["position"] = modelList;
            ViewData["hdMethod"] = addType;
            return View(dept);
        }

        /// <summary>
        /// 保存
        /// </summary>
        [HttpPost]
        public void Save()
        {

        }

        /// <summary>
        /// 获取人员数据
        /// </summary>
        /// <returns>返回人员数据视图</returns>
        public ActionResult EmpData()
        {
            BLL.HRDept deptBll = new BLL.HRDept();
            TreeModelList treeModelList = new TreeModelList();

            List<Model.HRDept> deptAllList = new List<Model.HRDept>();
            deptAllList = deptBll.GetListByEntityNo(GlobalConstant.ENTITYNO);
            if (deptAllList.Count > 0)
            {
                List<Model.HRDept> deptlist = (from c in deptAllList where c.FParentDeptNo == null orderby c.FSeq select c).ToList();

                foreach (Model.HRDept dept in deptlist)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = "^_^" + dept.FDeptNo,
                        Name = dept.FDeptName,
                        ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                    };

                    List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();
                    if (deptModelList.Count > 0)
                    {
                        GetChildGetChildDeptEmpl(deptModelList, treeParentItemModel, deptAllList);
                    }

                    GetEmpl(dept.FDeptNo, treeParentItemModel);

                    treeModelList.Add(treeParentItemModel);
                }

            }

            return View(treeModelList);
        }

        /// <summary>
        /// 递归获取部门数，绑定相关人员
        /// </summary>
        /// <param name="deptChildList">部门列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="deptAllList">部门集合</param>
        private void GetChildGetChildDeptEmpl(List<Model.HRDept> deptChildList, TreeItemModel treeParentItemModel, List<Model.HRDept> deptAllList)
        {
            foreach (Model.HRDept dept in deptChildList)
            {

                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = "^_^" + dept.FDeptNo,
                    Name = dept.FDeptName,
                    ImageUrl = Url.Content("~/Content/ControlImages/dept_Groups.png")
                };

                List<Model.HRDept> deptModelList = (from c in deptAllList where c.FParentDeptNo == dept.FDeptNo orderby c.FSeq select c).ToList();

                if (deptModelList.Count > 0)
                {
                    GetChildGetChildDeptEmpl(deptModelList, treeChildItemModel, deptAllList);
                }

                GetEmpl(dept.FDeptNo, treeChildItemModel);

            }

        }


        /// <summary>
        /// 获取人员数据
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        private void GetEmpl(string deptNo, TreeItemModel treeParentItemModel)
        {
            BLL.HREmplPosition emplBll = new BLL.HREmplPosition();
            DataSet ds = emplBll.GetDeptEmplList(deptNo, GlobalConstant.ENTITYNO);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                TreeItemModel treeChildItemModel = new TreeItemModel(treeParentItemModel)
                {
                    Id = row["FEmplNo"].ToString(),
                    Name = row["FEmpName"].ToString(),
                    ImageUrl = Url.Content("~/Content/ControlImages/user_male.png")
                };
            }
        }




        /// <summary>
        /// 部门岗位的Grid调用方法
        /// </summary>
        /// <returns>返回部门岗位Grid操作后视图</returns>
        public ActionResult PositionGrid()
        {
            //string callbackArg = DevExpress.Web.Mvc.DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            string deptNo = Request.Params["DeptNo"] == null ? string.Empty : Request.Params["DeptNo"];

            List<Model.HRPosition> modelList = new List<Model.HRPosition>();
            if (deptNo == string.Empty)//数据在未保存到数据
            {
                modelList = OperateSession.getList<Model.HRPosition>(GlobalConstant.PARAM_DEPTPOSITION);
            }
            else
            {
                BLL.HRPosition positionBll = new BLL.HRPosition();
                modelList = positionBll.GetListByDeptNo(deptNo, GlobalConstant.ENTITYNO);
                if (modelList.Count() > 0)
                {
                    modelList.ForEach(c => c.FPositionPropName = c.FPositionProp == "1" ? "正级(主持工作)" : c.FPositionProp == "2" ? "副级" : "普通");
                }
            }


            return PartialView("PositionGrid", modelList);

        }

       /// <summary>
       /// 增加岗位
       /// </summary>
       /// <param name="position">岗位信息</param>
       /// <returns>增加后Grid 视图</returns>
        public ActionResult InlineEditingAddNewPartial([ModelBinder(typeof(DevExpressEditorsBinder))] Model.HRPosition position)
        {
            List<Model.HRPosition> modelList = GetPositonAllList("");

            return PartialView("PositionGrid", (from c in modelList where c.FPositionNo == "abc" select c));

        }
        /// <summary>
        /// 更新岗位
        /// </summary>
        /// <param name="position">岗位信息</param>
        /// <returns>更新后Grid 视图</returns>
        public ActionResult InlineEditingUpdatePartial([ModelBinder(typeof(DevExpressEditorsBinder))] Model.HRPosition position)
        {
            List<Model.HRPosition> modelList = GetPositonAllList("");

            return PartialView("PositionGrid", (from c in modelList where c.FPositionNo == "006" select c));
        }

        /// <summary>
        /// 删除岗位
        /// </summary>
        /// <param name="fPositionNo">岗位编号</param>
        /// <returns>删除后Grid 视图</returns>
        public ActionResult InlineEditingDeletePartial(string fPositionNo)
        {
            List<Model.HRPosition> modelList = GetPositonAllList("");

            return PartialView("PositionGrid", (from c in modelList where c.FPositionNo == "006" select c));
        }

        /// <summary>
        /// 获取部门组织信息
        /// </summary>
        /// <param name="deptNo">单位编号</param>
        /// <returns>岗位列表</returns>
        private List<Model.HRPosition> GetPositonAllList(string deptNo)
        {
            List<Model.HRPosition> modelList = new List<Model.HRPosition>();
            Model.HRPosition model = new Model.HRPosition();
            model.FPositionNo = "abc";
            model.FPositionName = "123";
            model.FPositionProp = "1";
            model.FPositionPropName = "正级(主持工作)";
            model.FSeq = 1;
            //model.FDeptNo = "101";
            modelList.Add(model);

            model = new Model.HRPosition();
            model.FPositionNo = "006";
            model.FPositionName = "123";
            model.FPositionProp = "1";
            model.FSeq = 3;
            //model.FDeptNo = "101";
            model.FPositionPropName = "正级(主持工作)";
            modelList.Add(model);

            return modelList;
        }


        /// <summary>
        /// 岗位增加修改
        /// dept=null 数据在数据库中不存在 dept!=null 数据在数据库中已存在
        /// positionNo=null 新增   positionNo!=null 修改
        /// </summary>
        /// <param name="positionNo">岗位编号</param>
        /// <param name="deptno">部门编号</param>
        /// <returns>岗位增加修改视图</returns>
        public ActionResult PositionEdit(string positionNo, string deptno)
        {
            Model.HRPosition positionObj = new Model.HRPosition();

            if (deptno == null)
            {
                if (positionNo != null)
                {
                    List<Model.HRPosition> modelList = OperateSession.getList<Model.HRPosition>(GlobalConstant.PARAM_DEPTPOSITION);
                    var query = from c in modelList where c.FPositionNo == positionNo select c;
                    if (query.Count() > 0)
                    {
                        positionObj = query.First();
                    }
                    ViewData["bl_position"] = false;
                }
                else
                {
                    positionObj.FPositionProp = "9";
                    ViewData["bl_position"] = true;
                }
            }
            else
            {
                deptno = Fetch.estimateString(deptno, CheckGetEnum.Safety);
                if (positionNo != null)
                {
                    positionNo = Fetch.estimateString(positionNo, CheckGetEnum.Safety);

                    BLL.HRPosition positionBll = new BLL.HRPosition();
                    positionObj = positionBll.GetModel(positionNo, deptno, GlobalConstant.ENTITYNO);
                    if (null == positionObj)
                    {
                        positionObj = new Model.HRPosition();
                    }
                    ViewData["bl_position"] = false;
                }
                else
                {
                    positionObj.FDeptNo = deptno;
                    positionObj.FPositionProp = "9";
                    ViewData["bl_position"] = true;
                }
            }



            return View(positionObj);
        }

        /// <summary>
        /// 部门岗位增加修改保存
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="positionNo">岗位编号</param>
        /// <param name="positionName">岗位名称</param>
        /// <param name="prop">岗位性质</param>
        /// <param name="propName">性质名称</param>
        /// <param name="seq">序号</param>
        /// <param name="isAdd">true 新增 false 修改</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        public string PositionSave(string deptNo, string positionNo, string positionName, string prop, string propName, int seq, bool isAdd)
        {
            string str = string.Empty;

            positionNo = Fetch.estimateString(positionNo, CheckGetEnum.Safety);
            positionName = Fetch.estimateString(positionName, CheckGetEnum.Safety);
            prop = Fetch.estimateString(prop, CheckGetEnum.Safety);
            propName = Fetch.estimateString(propName, CheckGetEnum.Safety);

            Model.HRPosition model = new Model.HRPosition();
            if (string.IsNullOrEmpty(deptNo))//数据未保存到数据库
            {
                BLL.HRPosition HRPositionBLL=new BLL.HRPosition();
                List<Model.HRPosition> modelList = OperateSession.getList<Model.HRPosition>(GlobalConstant.PARAM_DEPTPOSITION);

                var query = from c in modelList where c.FPositionProp == "1" select c;
                if (prop == "1" && query.Count() > 0 && (isAdd || query.First().FPositionNo != positionNo))
                {
                    str = "prop1";//已存在正级岗位
                }
                else
                {
                    query = from c in modelList where c.FPositionNo == positionNo select c;
                    if (isAdd)
                    {
                        if (query.Count() > 0)
                        {
                            str = "exists"; //编号已存在
                        }
                        else
                        {
                            model.FPositionName = positionName;
                            model.FPositionNo = positionNo;
                            model.FPositionProp = prop;
                            model.FSeq = seq;
                            model.FPositionPropName = propName;
                            modelList.Add(model);
                            OperateSession.addList(GlobalConstant.PARAM_DEPTPOSITION, modelList);
                            str = "ok";
                        }
                    }
                    else
                    {
                        if (query.Count() > 0)
                        {
                            model = query.First();
                            model.FPositionName = positionName;
                            model.FPositionNo = positionNo;
                            model.FPositionProp = prop;
                            model.FSeq = seq;
                            model.FPositionPropName = propName;
                            str = "ok";
                        }
                        else
                        {
                            str = "no";//数据不存在
                        }

                    }
                }
            }
            else
            {
                deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);

                BLL.HRPosition positionBll = new BLL.HRPosition();
                string getPositonNo = positionBll.GetByPositionProp(deptNo, GlobalConstant.ENTITYNO, "1");

                if (prop == "1" && getPositonNo != string.Empty && (isAdd || getPositonNo != positionNo))
                {
                    str = "prop1";//已存在正级岗位
                }
                else
                {
                    bool bl = positionBll.Exists(positionNo, deptNo, GlobalConstant.ENTITYNO);
                    model.FDeptNo = deptNo;
                    model.FPositionName = positionName;
                    model.FPositionNo = positionNo;
                    model.FPositionProp = prop;
                    model.FSeq = seq;
                    model.FEntityNo = GlobalConstant.ENTITYNO;
                    if (isAdd)
                    {
                        if (bl)
                        {
                            str = "exists"; //编号已存在
                        }
                        else
                        {
                            try
                            {
                                positionBll.Add(model);
                                str = "ok";
                            }
                            catch (Exception)
                            {
                                str = "error";
                            }
                        }
                    }
                    else
                    {
                        if (bl)
                        {
                            try
                            {
                                positionBll.Update(model);
                                str = "ok";
                            }
                            catch (Exception)
                            {
                                str = "error";
                            }
                        }
                        else
                        {
                            str = "no";//数据不存在
                        }
                    }

                }

            }


            return str;
        }

        /// <summary>
        /// 删除部门岗位
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="positionNo">岗位编号</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        public string DelPosition(string deptNo, string positionNo)
        {
            string str = string.Empty;
            if (null != positionNo)
            {
                positionNo = Fetch.estimateString(positionNo, CheckGetEnum.Safety);

                if (string.IsNullOrEmpty(deptNo))//数据未保存到数据库
                {
                    List<Model.HRPosition> modelList = OperateSession.getList<Model.HRPosition>(GlobalConstant.PARAM_DEPTPOSITION);
                    var query = modelList.Where(c => c.FPositionNo == positionNo);
                    if (query.Count() > 0)
                    {
                        modelList.Remove(query.First());
                        str = "ok";
                    }
                    else
                    {
                        str = "no";
                    }
                }
                else
                {
                    deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);

                    BLL.HRPosition positionBll = new BLL.HRPosition();
                    try
                    {
                        positionBll.Delete(positionNo, deptNo, GlobalConstant.ENTITYNO);
                        str = "ok";
                    }
                    catch (Exception)
                    {
                        str = "error";
                    }
                }
            }

            return str;
        }

        /// <summary>
        /// 删除部门数据
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        public string DelDept(string deptNo)
        {
            string str = "";
            if (!string.IsNullOrEmpty(deptNo))
            {
                deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);

                //先判断部门是否存在人员
                BLL.HREmplPosition empPositionBll = new BLL.HREmplPosition();
                if (empPositionBll.ExistsDept(deptNo, GlobalConstant.ENTITYNO))//判断部门是否存在人员
                {
                    str = "existsEmp";
                }
                else
                {
                    BLL.HRDept deptBll = new BLL.HRDept();
                    try
                    {
                        deptBll.DeleteDeptAndPosition(GlobalConstant.ENTITYNO, deptNo);
                        str = "ok";
                    }
                    catch (Exception)
                    {
                        str = "error";
                    }
                }
            }

            return str;
        }


        /// <summary>
        /// 判断部门编号是否存在
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <returns>是否存在</returns>
        [HttpPost]
        public string ExistsDept(string deptNo)
        {
            string str = "";
            deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);
            BLL.HRDept deptBll = new BLL.HRDept();
            if (deptBll.Exists(GlobalConstant.ENTITYNO, deptNo))
            {
                str = "Y";
            }
            else
            {
                str = "N";
            }
            return str;
        }
        /// <summary>
        /// 部门同级排序
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <returns>视图</returns>
        public ActionResult DeptSeq(string deptNo)
        {
            BLL.HRDept deptBLL = new BLL.HRDept();
            string entityNo = GlobalConstant.ENTITYNO;
            Model.HRDept hrDeptModel =deptBLL.GetModel(entityNo, deptNo);
            ViewData["docListJson"] = "";
            ViewData["focusId"] = deptNo;
            if (hrDeptModel != null)
            {
                List<Model.HRDept> hrDeptList = deptBLL.GetListByEntityNo(entityNo).Where(s => s.FParentDeptNo == hrDeptModel.FParentDeptNo).ToList();
                ViewData["listJson"] = JsonConvert.SerializeObject(hrDeptList);
            }
            return View();
        }
        /// <summary>
        /// 排序保存
        /// </summary>
        /// <param name="deptNos">排序的部门编号</param>
        /// <returns></returns>
        public string SaveRearrange(string deptNos)
        {
            string result = "FAILED";
            string errorMessage = "";
            string entityNo = GlobalConstant.ENTITYNO;
            string[] deptNoArray = deptNos.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                BLL.HRDept deptBLL = new BLL.HRDept();
                deptBLL.SeqDeptTop(deptNoArray, entityNo);
                result = "SUCCESS";
            }
            catch (Exception ee)
            {
                errorMessage = "排序保存失败" + ee.Message;
            }


            if (result == "FAILED")
            {
                if (errorMessage == "")
                {
                    errorMessage = "错误";
                }
                result += "|" + errorMessage;
            }
            return result;
        }
        /// <summary>
        /// 部门增加修改保存
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="deptName">部门名称</param>
        /// <param name="seq">序号</param>
        /// <param name="parDeptNo">上级部门名称</param>
        /// <param name="deptProp">部门性质</param>
        /// <param name="isAgency">是否排除机构</param>
        /// <param name="chargeLeader">分管领导</param>
        /// <param name="type">增加或修改</param>
        /// <returns>返回增加或修改结果</returns>
        [HttpPost]
        public string DeptSave(string deptNo, string deptName, int seq, string parDeptNo, string deptProp, string isAgency, string chargeLeader, string type, string FDeptShorName,
            string isReportPlanSummary, string isCheckSystem)
        {
            string str = string.Empty;

            Model.HRDept deptObj = new Model.HRDept();
            deptObj.FChargeLeader = Fetch.estimateString(chargeLeader, CheckGetEnum.Safety);
            deptObj.FDeptName = Fetch.estimateString(deptName, CheckGetEnum.Safety);
            deptObj.FDeptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);
            deptObj.FDeptProp = Fetch.estimateString(deptProp, CheckGetEnum.Safety);
            deptObj.FEntityNo = GlobalConstant.ENTITYNO;
            deptObj.FIsAgency = Fetch.estimateString(isAgency, CheckGetEnum.Safety);
            deptObj.FDeptShorName = FDeptShorName;
            deptObj.FIsEval = "Y";
            if (parDeptNo != string.Empty)
            {
                deptObj.FParentDeptNo = Fetch.estimateString(parDeptNo, CheckGetEnum.Safety);
            }
            deptObj.FSeq = seq;

            deptObj.FIsReportPlanSummary = isReportPlanSummary == "true" ? "Y" : "N";
            deptObj.FIsCheckSystem = isCheckSystem == "true" ? "Y" : "N";

            BLL.HRDept deptBll = new BLL.HRDept();
            try
            {
                if (type == "add")
                {
                    List<Model.HRPosition> positionList = OperateSession.getList<Model.HRPosition>(GlobalConstant.PARAM_DEPTPOSITION);
                    deptBll.AddDeptAndPosition(deptObj, positionList);
                }
                else if (type == "edit")
                {
                    deptBll.UpdatePart(deptObj);
                }


                OperateSession.del(GlobalConstant.PARAM_DEPTPOSITION);//清空岗位Session数据
                str = "ok";

            }
            catch (Exception)
            {
                str = "error";
            }


            return str;
        }



    }
}
